package cn.tx.service.impl;

import cn.tx.constant.RedisMessageConstant;
import cn.tx.mapper.MemberDao;
import cn.tx.pojo.Member;
import cn.tx.service.LoginService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

/**
 * @author Snake Eyes
 * @PROJECT_NAME: health_parent
 * @DESCRIPTION:
 * @USER: 11647
 * @DATE: 2021/9/7 22:43
 * @Email: 1164763541@qq.com
 */
@Service(interfaceClass = LoginService.class)
@Transactional
public class LoginServiceImpl implements LoginService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private MemberDao memberDao;

    @Override
    public void login(Map<String, Object> loginInfo) {

        //获取验证码
        String redisCode = (String) redisTemplate.boundValueOps(loginInfo.get("telephone").toString() + RedisMessageConstant.SENDTYPE_LOGIN).get();
        //判断验证码是否正确
        if (redisCode.equals(loginInfo.get("validateCode").toString())) {
            //查询是否存在会员
            Member byTelephone = memberDao.findByTelephone(loginInfo.get("telephone").toString());
            //存在会员

            if (byTelephone != null) {
                //修改密码

                //登录
                return;

            } else {
                Member member = new Member();
                //不存在会员
                System.out.println(loginInfo.get("telephone").toString());

                String phone = loginInfo.get("telephone").toString();


                member.setPhoneNumber(phone);
                member.setRegTime(new Date());
                member.setPassword(loginInfo.get("validateCode").toString());
                //创建会员
                memberDao.save(member);
                //登录
                return;

            }

        }
    }
}
